{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 常用模块\n",
    "- calendar\n",
    "- time\n",
    "- datetime\n",
    "- timeit\n",
    "- os\n",
    "- shutil\n",
    "- zip\n",
    "- math\n",
    "- string\n",
    "- 上述所有模块使用理论上都应该先导入，string是特例\n",
    "- calendar，time，datetime的区别参考中文意思"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# calendar\n",
    "- 跟日历相关的模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 使用需要先导入\n",
    "import calendar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n",
      "                                  2017\n",
      "\n",
      "      January                   February                   March\n",
      "Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su\n",
      "                   1             1  2  3  4  5             1  2  3  4  5\n",
      " 2  3  4  5  6  7  8       6  7  8  9 10 11 12       6  7  8  9 10 11 12\n",
      " 9 10 11 12 13 14 15      13 14 15 16 17 18 19      13 14 15 16 17 18 19\n",
      "16 17 18 19 20 21 22      20 21 22 23 24 25 26      20 21 22 23 24 25 26\n",
      "23 24 25 26 27 28 29      27 28                     27 28 29 30 31\n",
      "30 31\n",
      "\n",
      "       April                      May                       June\n",
      "Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su\n",
      "                1  2       1  2  3  4  5  6  7                1  2  3  4\n",
      " 3  4  5  6  7  8  9       8  9 10 11 12 13 14       5  6  7  8  9 10 11\n",
      "10 11 12 13 14 15 16      15 16 17 18 19 20 21      12 13 14 15 16 17 18\n",
      "17 18 19 20 21 22 23      22 23 24 25 26 27 28      19 20 21 22 23 24 25\n",
      "24 25 26 27 28 29 30      29 30 31                  26 27 28 29 30\n",
      "\n",
      "        July                     August                  September\n",
      "Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su\n",
      "                1  2          1  2  3  4  5  6                   1  2  3\n",
      " 3  4  5  6  7  8  9       7  8  9 10 11 12 13       4  5  6  7  8  9 10\n",
      "10 11 12 13 14 15 16      14 15 16 17 18 19 20      11 12 13 14 15 16 17\n",
      "17 18 19 20 21 22 23      21 22 23 24 25 26 27      18 19 20 21 22 23 24\n",
      "24 25 26 27 28 29 30      28 29 30 31               25 26 27 28 29 30\n",
      "31\n",
      "\n",
      "      October                   November                  December\n",
      "Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su\n",
      "                   1             1  2  3  4  5                   1  2  3\n",
      " 2  3  4  5  6  7  8       6  7  8  9 10 11 12       4  5  6  7  8  9 10\n",
      " 9 10 11 12 13 14 15      13 14 15 16 17 18 19      11 12 13 14 15 16 17\n",
      "16 17 18 19 20 21 22      20 21 22 23 24 25 26      18 19 20 21 22 23 24\n",
      "23 24 25 26 27 28 29      27 28 29 30               25 26 27 28 29 30 31\n",
      "30 31\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# calendar：　获取一年的日历字符串\n",
    "# 参数\n",
    "# w = 每个日期之间的间隔字符数\n",
    "# l = 每周所占用的行数\n",
    "# c = 每个月之间的间隔字符数\n",
    "cal = calendar.calendar(2017)\n",
    "print(type(cal))\n",
    "print(cal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                 2017\n",
      "\n",
      "      January                  February                  March\n",
      "Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su\n",
      "                   1            1  2  3  4  5            1  2  3  4  5\n",
      " 2  3  4  5  6  7  8      6  7  8  9 10 11 12      6  7  8  9 10 11 12\n",
      " 9 10 11 12 13 14 15     13 14 15 16 17 18 19     13 14 15 16 17 18 19\n",
      "16 17 18 19 20 21 22     20 21 22 23 24 25 26     20 21 22 23 24 25 26\n",
      "23 24 25 26 27 28 29     27 28                    27 28 29 30 31\n",
      "30 31\n",
      "\n",
      "       April                     May                      June\n",
      "Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su\n",
      "                1  2      1  2  3  4  5  6  7               1  2  3  4\n",
      " 3  4  5  6  7  8  9      8  9 10 11 12 13 14      5  6  7  8  9 10 11\n",
      "10 11 12 13 14 15 16     15 16 17 18 19 20 21     12 13 14 15 16 17 18\n",
      "17 18 19 20 21 22 23     22 23 24 25 26 27 28     19 20 21 22 23 24 25\n",
      "24 25 26 27 28 29 30     29 30 31                 26 27 28 29 30\n",
      "\n",
      "        July                    August                 September\n",
      "Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su\n",
      "                1  2         1  2  3  4  5  6                  1  2  3\n",
      " 3  4  5  6  7  8  9      7  8  9 10 11 12 13      4  5  6  7  8  9 10\n",
      "10 11 12 13 14 15 16     14 15 16 17 18 19 20     11 12 13 14 15 16 17\n",
      "17 18 19 20 21 22 23     21 22 23 24 25 26 27     18 19 20 21 22 23 24\n",
      "24 25 26 27 28 29 30     28 29 30 31              25 26 27 28 29 30\n",
      "31\n",
      "\n",
      "      October                  November                 December\n",
      "Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su\n",
      "                   1            1  2  3  4  5                  1  2  3\n",
      " 2  3  4  5  6  7  8      6  7  8  9 10 11 12      4  5  6  7  8  9 10\n",
      " 9 10 11 12 13 14 15     13 14 15 16 17 18 19     11 12 13 14 15 16 17\n",
      "16 17 18 19 20 21 22     20 21 22 23 24 25 26     18 19 20 21 22 23 24\n",
      "23 24 25 26 27 28 29     27 28 29 30              25 26 27 28 29 30 31\n",
      "30 31\n",
      "\n"
     ]
    }
   ],
   "source": [
    "cal = calendar.calendar(2017, l=0, c=5)\n",
    "print(cal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# isleap: 判断某一年是否闰年\n",
    "calendar.isleap(2000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# leapdays: 获取指定年份之间的闰年的个数\n",
    "calendar.leapdays(2001, 1998)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function leapdays in module calendar:\n",
      "\n",
      "leapdays(y1, y2)\n",
      "    Return number of leap years in range [y1, y2).\n",
      "    Assume y1 <= y2.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(calendar.leapdays)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     March 2018\n",
      "Mo Tu We Th Fr Sa Su\n",
      "          1  2  3  4\n",
      " 5  6  7  8  9 10 11\n",
      "12 13 14 15 16 17 18\n",
      "19 20 21 22 23 24 25\n",
      "26 27 28 29 30 31\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# month（） 获取某个月的日历字符串\n",
    "# 格式:calendar.month(年，月)\n",
    "# 回值：月日历的字符串\n",
    "m3 = calendar.month(2018, 3)\n",
    "print(m3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "31\n"
     ]
    }
   ],
   "source": [
    "# monthrange（） 获取一个月的周几开始即和天数\n",
    "# 格式：calendar.monthrange(年,月)\n",
    "# 回值：元组(周几开始,总天数)\n",
    "# 注意：周默认 0 -6 表示周一到周天\n",
    "w,t = calendar.monthrange(2017, 3)\n",
    "print(w)\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "[[0, 0, 0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24, 25], [26, 27, 28, 29, 30, 31, 0]]\n"
     ]
    }
   ],
   "source": [
    "# monthcalendar() 返回一个月每天的矩阵列表\n",
    "# 格式：calendar.monthcalendar(年，月)\n",
    "# 回值：二级列表\n",
    "# 注意：矩阵中没有天数用0表示\n",
    "m = calendar.monthcalendar(2018, 3)\n",
    "print(type(m))\n",
    "print(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method pryear in module calendar:\n",
      "\n",
      "pryear(theyear, w=0, l=0, c=6, m=3) method of calendar.TextCalendar instance\n",
      "    Print a year's calendar.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# prcal: print calendar 直接打印日历\n",
    "#calendar.prcal(2018)\n",
    "help(calendar.prcal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     March 2018\n",
      "Mo Tu We Th Fr Sa Su\n",
      "          1  2  3  4\n",
      " 5  6  7  8  9 10 11\n",
      "12 13 14 15 16 17 18\n",
      "19 20 21 22 23 24 25\n",
      "26 27 28 29 30 31\n"
     ]
    }
   ],
   "source": [
    "# prmonth() 直接打印整个月的日历\n",
    "# 格式：calendar.prmonth(年，月)\n",
    "# 返回值：无\n",
    "calendar.prmonth(2018, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# weekday() 获取周几\n",
    "# 格式:calendar.weekday(年，月，日)\n",
    "# 返回值:周几对应的数字\n",
    "calendar.weekday(2018, 3, 26)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# time模块\n",
    "### 时间戳\n",
    "    - 一个时间表示，根据不同语言，可以是整数或者浮点数\n",
    "    - 是从1970年1月1日0时0分0秒到现在经历的秒数\n",
    "    - 如果表示的时间是1970年以前或者太遥远的未来，可能出现异常\n",
    "    - 32位操作系统能够支持到2038年\n",
    "\n",
    "### UTC时间\n",
    "    - UTC又称为世界协调时间，以英国的格林尼治天文所在地区的时间作为参考的时间，也叫做世界标准时间。\n",
    "    - 中国时间是 UTC+8 东八区\n",
    "  \n",
    "### 夏令时\n",
    "    - 夏令时就是在夏天的时候将时间调快一小时，本意是督促大家早睡早起节省蜡烛！ 每天变成25个小时，本质没变还是24小时\n",
    "    \n",
    "### 时间元组 \n",
    "    - 一个包含时间内容的普通元组\n",
    "    \n",
    "    \n",
    "        索引      内容    属性            值\n",
    "\n",
    "        0       年       tm_year     2015\n",
    "        1       月       tm_mon      1～12\n",
    "        2       日       tm_mday     1～31\n",
    "        3       时       tm_hour     0～23\n",
    "        4       分       tm_min      0～59\n",
    "        5       秒       tm_sec      0～61  60表示闰秒  61保留值\n",
    "        6       周几     tm_wday     0～6\n",
    "        7       第几天    tm_yday     1～366\n",
    "        8       夏令时    tm_isdst    0，1，-1（表示夏令时）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 需要单独导入\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "# 时间模块的属性\n",
    "# timezone: 当前时区和UTC时间相差的秒数，在没有夏令时的情况下的间隔,东八区的是 -28800\n",
    "# altzone  获取当前时区与UTC时间相差的秒数，在有夏令时的情况下，\n",
    "# daylight 测当前是否是夏令时时间状态, 0 表示是\n",
    "print(time.daylight)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1522068038.0160315"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 得到时间戳\n",
    "time.time()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20\n"
     ]
    }
   ],
   "source": [
    "# localtime， 得到当前时间的时间结构\n",
    "# 可以通过点号操作符得到相应的属性元素的内容\n",
    "t = time.localtime()\n",
    "print(t.tm_hour)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n",
      "Mon Mar 26 20:45:09 2018\n"
     ]
    }
   ],
   "source": [
    "#asctime() 返回元组的正常字符串化之后的时间格式 \n",
    "# 格式：time.asctime（时间元组）\n",
    "# 返回值:字符串 Tue Jun  6 11:11:00 2017\n",
    "t = time.localtime()\n",
    "\n",
    "tt = time.asctime(t)\n",
    "print(type(tt))\n",
    "print(tt)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n",
      "Mon Mar 26 20:46:30 2018\n"
     ]
    }
   ],
   "source": [
    "# ctime: 获取字符串化的当前时间\n",
    "t = time.ctime()\n",
    "print(type(t))\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'float'>\n",
      "1522068497.0\n"
     ]
    }
   ],
   "source": [
    "# mktime() 使用时间元组获取对应的时间戳\n",
    "# 格式：time.mktime（时间元组）\n",
    "# 返回值：浮点数时间戳\n",
    "\n",
    "lt = time.localtime()\n",
    "ts = time.mktime(lt)\n",
    "print(type(ts))\n",
    "print(ts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# clock: 获取cpu时间， 3.0-3.3版本直接使用, 3.6调用有问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "# sleep: 使程序进入睡眠，n秒后继续\n",
    "\n",
    "for i in range(10):\n",
    "    print(i)\n",
    "    time.sleep(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0013730000000000686\n"
     ]
    }
   ],
   "source": [
    "def p():\n",
    "    time.sleep(2.5)\n",
    "    \n",
    "t0 = time.clock()\n",
    "#p()\n",
    "time.sleep(3)\n",
    "t1 = time.clock()\n",
    "\n",
    "print(t1 - t0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018年03月26日 21:08\n"
     ]
    }
   ],
   "source": [
    "# strftime:将时间元组转化为自定义的字符串格式\n",
    "'''\n",
    "格式  含义  备注\n",
    "%a  本地（locale）简化星期名称    \n",
    "%A  本地完整星期名称    \n",
    "%b  本地简化月份名称    \n",
    "%B  本地完整月份名称    \n",
    "%c  本地相应的日期和时间表示    \n",
    "%d  一个月中的第几天（01 - 31）   \n",
    "%H  一天中的第几个小时（24 小时制，00 - 23）   \n",
    "%I  一天中的第几个小时（12 小时制，01 - 12）   \n",
    "%j  一年中的第几天（001 - 366）  \n",
    "%m  月份（01 - 12） \n",
    "%M  分钟数（00 - 59）    \n",
    "%p  本地 am 或者 pm 的相应符    注1\n",
    "%S  秒（01 - 61）  注2\n",
    "%U  一年中的星期数（00 - 53 星期天是一个星期的开始）第一个星期天之前的所有天数都放在第 0 周   注3\n",
    "%w  一个星期中的第几天（0 - 6，0 是星期天） 注3\n",
    "%W  和 %U 基本相同，不同的是 %W 以星期一为一个星期的开始  \n",
    "%x  本地相应日期  \n",
    "%X  本地相应时间  \n",
    "%y  去掉世纪的年份（00 - 99）    \n",
    "%Y  完整的年份   \n",
    "%z  用 +HHMM 或 -HHMM 表示距离格林威治的时区偏移（H 代表十进制的小时数，M 代表十进制的分钟数）      \n",
    "%%  %号本身\n",
    "'''\n",
    "# 把时间表示成， 2018年3月26日 21:05\n",
    "t = time.localtime()\n",
    "ft = time.strftime(\"%Y年%m月%d日 %H:%M\" , t)\n",
    "print(ft)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# datetime模块\n",
    "- datetinme提供日期和时间的运算和表示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-03-26\n",
      "26\n",
      "2018\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# datetime常见属性\n",
    "# datetime.date: 一个理想和的日期，提供year, month, day属性\n",
    "dt = datetime.date(2018, 3,26)\n",
    "print(dt)\n",
    "print(dt.day)\n",
    "print(dt.year)\n",
    "print(dt.month)\n",
    "\n",
    "# datetime.time: 提供一个理想和的时间， 居于哦hour， minute，sec，microsec等内容\n",
    "# datetime.datetime: 提供日期跟时间的组合\n",
    "# datetime.timedelta: 提供一个时间差，时间长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-03-26 21:17:44.743093\n",
      "2018-03-26 21:17:44.749105\n",
      "2018-03-26 21:17:44.749370\n"
     ]
    }
   ],
   "source": [
    "# datetime.datetime\n",
    "from datetime import datetime\n",
    "# 常用类方法：\n",
    "# today： \n",
    "# now\n",
    "# utcnow\n",
    "# fromtimestamp： 从时间戳中返回本地时间\n",
    "dt = datetime(2018, 3, 26)\n",
    "print(dt.today())\n",
    "print(dt.now())\n",
    "\n",
    "print(dt.fromtimestamp(time.time()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-03-26 21:21:35\n",
      "2018-03-26 22:21:35\n"
     ]
    }
   ],
   "source": [
    "# datetime.timedelta \n",
    "# 表示一个时间间隔\n",
    "\n",
    "from datetime import datetime, timedelta\n",
    "\n",
    "t1 = datetime.now()\n",
    "print( t1.strftime(\"%Y-%m-%d %H:%M:%S\"))\n",
    "# td表示以小时的时间长度\n",
    "td = timedelta(hours=1)\n",
    "# 当前时间加上时间间隔后，把得到的一个小时后的时间格式化输出\n",
    "print( (t1+td).strftime(\"%Y-%m-%d %H:%M:%S\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.604551076889038\n"
     ]
    }
   ],
   "source": [
    "# timeit-时间测量工具\n",
    "\n",
    "# 测量程序运行时间间隔实验\n",
    "def p():\n",
    "    time.sleep(3.6)\n",
    "    \n",
    "t1 = time.time()\n",
    "p()\n",
    "print(time.time() - t1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.6834080209991953\n",
      "6.945136217000254\n"
     ]
    }
   ],
   "source": [
    "import timeit\n",
    "# 生成列表两种方法的比较\n",
    "# 如果单纯比较生成一个列表的时间，可能很难实现\n",
    "c = '''\n",
    "sum = []\n",
    "for i in range(1000):\n",
    "    sum.append(i)\n",
    "'''\n",
    " \n",
    "# 利用timeit调用代码，执行100000次，查看运行时间\n",
    "t1= timeit.timeit(stmt=\"[i for i in range(1000)]\", number=100000 )\n",
    "# 测量代码c执行100000次运行结果\n",
    "t2 = timeit.timeit(stmt=c, number=100000)\n",
    "print(t1)\n",
    "print(t2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Repeat for 0\n",
      "Repeat for 1\n",
      "Repeat for 2\n",
      "Repeat for 0\n",
      "Repeat for 1\n",
      "Repeat for 2\n",
      "Repeat for 0\n",
      "Repeat for 1\n",
      "Repeat for 2\n",
      "Repeat for 0\n",
      "Repeat for 1\n",
      "Repeat for 2\n",
      "Repeat for 0\n",
      "Repeat for 1\n",
      "Repeat for 2\n",
      "Repeat for 0\n",
      "Repeat for 1\n",
      "Repeat for 2\n",
      "Repeat for 0\n",
      "Repeat for 1\n",
      "Repeat for 2\n",
      "Repeat for 0\n",
      "Repeat for 1\n",
      "Repeat for 2\n",
      "Repeat for 0\n",
      "Repeat for 1\n",
      "Repeat for 2\n",
      "Repeat for 0\n",
      "Repeat for 1\n",
      "Repeat for 2\n",
      "0.00976040500063391\n"
     ]
    }
   ],
   "source": [
    "# timeit 可以执行一个函数，来测量一个函数的执行时间 \n",
    "def doIt():\n",
    "    num = 3\n",
    "    for i in range(num):\n",
    "        print(\"Repeat for {0}\".format(i))\n",
    "       \n",
    "# 执行函数，重复10次\n",
    "t = timeit.timeit(stmt=doIt, number=10)\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Repeat for 0\n",
      "Repeat for 1\n",
      "Repeat for 2\n",
      "Repeat for 0\n",
      "Repeat for 1\n",
      "Repeat for 2\n",
      "Repeat for 0\n",
      "Repeat for 1\n",
      "Repeat for 2\n",
      "Repeat for 0\n",
      "Repeat for 1\n",
      "Repeat for 2\n",
      "Repeat for 0\n",
      "Repeat for 1\n",
      "Repeat for 2\n",
      "Repeat for 0\n",
      "Repeat for 1\n",
      "Repeat for 2\n",
      "Repeat for 0\n",
      "Repeat for 1\n",
      "Repeat for 2\n",
      "Repeat for 0\n",
      "Repeat for 1\n",
      "Repeat for 2\n",
      "Repeat for 0\n",
      "Repeat for 1\n",
      "Repeat for 2\n",
      "Repeat for 0\n",
      "Repeat for 1\n",
      "Repeat for 2\n",
      "0.012599870999110863\n"
     ]
    }
   ],
   "source": [
    "s = '''\n",
    "def doIt(num):\n",
    "    for i in range(num):\n",
    "        print(\"Repeat for {0}\".format(i))\n",
    "\n",
    "'''\n",
    "# 执行doIt(num)\n",
    "# setup负责把环境变量准备好\n",
    "# 实际相当于给timeit创造了一个小环境\n",
    "# 在创作的小环境中， 代码执行的顺序大致是\n",
    "# \n",
    "'''\n",
    "def doIt(num):\n",
    "    .....\n",
    "    \n",
    "num = 3\n",
    "\n",
    "doIt(num)\n",
    "'''\n",
    "\n",
    "t = timeit.timeit(\"doIt(num)\", setup=s+\"num=3\", number=10)\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# datetime.datetime 模块\n",
    "- 提供比较好用的时间而已\n",
    "- 类定义\n",
    "\n",
    "       class datetime.datetime(year, month, day[, hour\n",
    "                [, minute\n",
    "                [, second\n",
    "                [, microsecond\n",
    "                [, tzinfo]]]]])\n",
    "      # The year, month and day arguments are required.\n",
    "      MINYEAR <= year <= MAXYEAR\n",
    "      1 <= month <= 12\n",
    "      1 <= day <= n\n",
    "      0 <= hour < 24\n",
    "      0 <= minute < 60\n",
    "      0 <= second < 60\n",
    "      0 <= microsecond < 10**\n",
    "- 类方法\n",
    "\n",
    "`               \n",
    "datetime.today(): 返回当前本地datetime.随着 tzinfo None. datetime.fromtimestamp(time.time()).\n",
    "datetime.now([tz]): 返回当前本地日期和时间, 如果可选参数tz为None或没有详细说明,这个方法会像today().\n",
    "datetime.utcnow(): 返回当前的UTC日期和时间, 如果tzinfo None ,那么与now()类似.\n",
    "datetime.fromtimestamp(timestamp[, tz]): 根据时间戳返回本地的日期和时间.tz指定时区.\n",
    "datetime.utcfromtimestamp(timestamp): 根据时间戳返回 UTC datetime.\n",
    "datetime.fromordinal(ordinal): 根据Gregorian ordinal 返回datetime.\n",
    "datetime.combine(date, time): 根据date和time返回一个新的datetime.\n",
    "datetime.strptime(date_string, format): 根据date_string和format返回一个datetime.\n",
    "\n",
    "实例方法\n",
    "\n",
    "datetime.date(): 返回相同年月日的date对象.\n",
    "datetime.time(): 返回相同时分秒微秒的time对象.\n",
    "datetime.replace(kw): kw in [year, month, day, hour, minute, second, microsecond, tzinfo], 与date类似.\n",
    "类属性\n",
    "\n",
    "datetime.min: datetime(MINYEAR, 1, 1).\n",
    "datetime.max: datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999).\n",
    "\n",
    "实例属性(read-only)\n",
    "\n",
    "datetime.year: 1 至 9999\n",
    "datetime.month: 1 至 12\n",
    "datetime.day: 1 至 n\n",
    "datetime.hour: In range(24). 0 至 23\n",
    "datetime.minute: In range(60).\n",
    "datetime.second: In range(60).\n",
    "datetime.microsecond: In range(1000000).\n",
    "`\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-03-28 20:09:58.926144\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime as dt\n",
    "\n",
    "print(dt.now())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# os - 操作系统相关\n",
    "- 跟操作系统相关，主要是文件操作\n",
    "- 于系统相关的操作，主要包含在三个模块里\n",
    "    - os， 操作系统目录相关\n",
    "    - os.path, 系统路径相关操作\n",
    "    - shutil， 高级文件操作，目录树的操作，文件赋值，删除，移动\n",
    "- 路径：\n",
    "    - 绝对路径： 总是从根目录上开始\n",
    "    - 相对路径： 基本以当前环境为开始的一个相对的地方\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# os 模块\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/tlxy/cookbook_and_code\n"
     ]
    }
   ],
   "source": [
    "# getcwd() 获取当前的工作目录\n",
    "# 格式：os.getcwd()\n",
    "# 返回值：当前工作目录的字符串\n",
    "# 当前工作目录就是程序在进行文件相关操作，默认查找文件的目录\n",
    "\n",
    "mydir = os.getcwd()\n",
    "print(mydir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/tlxy\n"
     ]
    }
   ],
   "source": [
    "# chdir() 改变当前的工作目录\n",
    "# change directory\n",
    "#  格式：os.chdir（路径）\n",
    "#  返回值：无\n",
    "\n",
    "os.chdir('/home/tlxy')\n",
    "mydir = os.getcwd()\n",
    "print(mydir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['.xsession-errors.old', '.kingsoft', '06-高级语法-常用模块.ipynb', '.sudo_as_admin_successful', '.bashrc', '.ICEauthority', '.sunpinyin', '图片', '.profile', '.conda', '.java', '.presage', '.pki', '模板', '.sogouinput', '.config', 'tulingxueyuan', '.astropy', '.Xdefaults', '.gitconfig', '音乐', '视频', 'cookbook_and_code', '.bash_history', '.dmrc', '下载', '.vboxclient-clipboard.pid', '.vboxclient-draganddrop.pid', '.PyCharm2017.3', '.bash_logout', '.gconf', '.local', 'ddd', '.xsession-errors', '.mozilla', '.jupyter', '.gnupg', '.ipynb_checkpoints', '.thumbnails', '2-oop.ipynb', '文档', '公共的', '.cache', '.ipython', '桌面', '.vboxclient-seamless.pid', '.vboxclient-display.pid', '.xscreensaver', '.xinputrc', '.Xauthority', 'oop-2.ipynb']\n"
     ]
    }
   ],
   "source": [
    "# listdir() 获取一个目录中所有子目录和文件的名称列表\n",
    "#  格式:os.listdir(路径)\n",
    "#  返回值：所有子目录和文件名称的列表\n",
    "\n",
    "ld = os.listdir()\n",
    "print(ld)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "# makedirs（） 递归创建文件夹\n",
    "# 格式：os.makedirs(递归路径)\n",
    "# 返回值：无\n",
    "#  递归路径：多个文件夹层层包含的路径就是递归路径 例如 a/b/c...\n",
    "\n",
    "rst = os.makedirs(\"dana\")\n",
    "print(rst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "# system() 运行系统shell命令\n",
    "# 格式：os.system(系统命令)\n",
    "#  返回值：打开一个shell或者终端界面\n",
    "# 一般推荐使用subprocess代替\n",
    "\n",
    "# ls是列出当前文件和文件夹的系统命令\n",
    "rst = os.system(\"ls\")\n",
    "print(rst)\n",
    "\n",
    "# 在当前目录下创建一个dana.haha 的文集\n",
    "rst = os.system(\"touch dana.haha\")\n",
    "print(rst)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/var/sw/anaconda3/bin:/home/tlxy/bin:/home/tlxy/.local/bin:/var/sw/anaconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin\n"
     ]
    }
   ],
   "source": [
    "#getenv() 获取指定的系统环境变量值\n",
    "# 相应的还有putenv\n",
    "#  格式：os.getenv('环境变量名')\n",
    "#  返回值：指定环境变量名对应的值\n",
    "rst = os.getenv(\"PATH\")\n",
    "print(rst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# exit() 退出当前程序\n",
    "#  格式：exit()\n",
    "#  返回值:无\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 值部分\n",
    "- os.curdir: curretn dir,当前目录\n",
    "- os.pardir: parent dir， 父亲目录\n",
    "- os.sep: 当前系统的路径分隔符\n",
    "    - windows: \"\\\"\n",
    "    - linux: \"/\"\n",
    "- os.linesep: 当前系统的换行符号\n",
    "    - windows: \"\\r\\n\"\n",
    "    - unix,linux,macos: \"\\n\"\n",
    "- os.name： 当前系统名称\n",
    "    - windows： nt\n",
    "    - mac，unix，linux： posix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "..\n",
      ".\n"
     ]
    }
   ],
   "source": [
    "print(os.pardir)\n",
    "print(os.curdir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(os.sep)\n",
    "print(os.linesep)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/tlxy/dana\n"
     ]
    }
   ],
   "source": [
    "# 在路径相关的操作中，不要手动拼写地址，因为手动拼写的路径可能不具有移植性\n",
    "path = \"/home/tlxy\" + \"/\" + \"dana\"\n",
    "print(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "posix\n"
     ]
    }
   ],
   "source": [
    "# linux操作系统的名称是posix\n",
    "print(os.name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# os.path 模块， 跟路径相关的模块\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os.path as op"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/tlxy\n"
     ]
    }
   ],
   "source": [
    "# abspath() 将路径转化为绝对路径\n",
    "# abselute 绝对\n",
    "#  格式:os.path.abspath('路径')\n",
    "#  返回值：路径的绝对路径形式\n",
    "\n",
    "# linux中\n",
    "# . 点号，代表当前目录\n",
    "# .. 双点，代表父目录\n",
    "absp = op.abspath(\".\")\n",
    "print(absp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tlxy\n",
      "dana.haha\n"
     ]
    }
   ],
   "source": [
    "# basename() 获取路径中的文件名部分\n",
    "#  格式:os.path.basename(路径)\n",
    "#  返回值：文件名字符串\n",
    "\n",
    "bn = op.basename(\"/home/tlxy\")\n",
    "print(bn)\n",
    "bn = op.basename(\"/home/tlxy\")\n",
    "print(bn)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/tlxy/dana.haha\n"
     ]
    }
   ],
   "source": [
    "# join() 将多个路径拼合成一个路径\n",
    "#  格式：os.path.join(路径1，路径2....)\n",
    "#  返回值：组合之后的新路径字符串\n",
    "\n",
    "bd = \"/home/tlxy\"\n",
    "fn = \"dana.haha\"\n",
    "\n",
    "p = op.join(bd, fn)\n",
    "print(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('/home/tlxy', 'dana.haha')\n",
      "/home/tlxy dana.haha\n"
     ]
    }
   ],
   "source": [
    "# split() 将路径切割为文件夹部分和当前文件部分\n",
    "#  格式:os.path.split（路径）\n",
    "#  返回值：路径和文件名组成的元组\n",
    "\n",
    "t = op.split(\"/home/tlxy/dana.haha\")\n",
    "print(t)\n",
    "\n",
    "d,p = op.split(\"/home/tlxy/dana.haha\")\n",
    "print(d, p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# isdir（） 检测是否是目录\n",
    "#  格式：os.path.isdir(路径)\n",
    "#  返回值：布尔值\n",
    "\n",
    "rst = op.isdir(\"/home/tlxy/dana.haha\")\n",
    "rst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# exists() 检测文件或者目录是否存在\n",
    "#  格式：os.path.exists(路径)\n",
    "#  返回值:布尔值\n",
    "\n",
    "e = op.exists(\"/home/tlxy/haha\")\n",
    "e"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# shutil 模块\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import shutil"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/tlxy/haha.haha\n"
     ]
    }
   ],
   "source": [
    "# copy() 复制文件\n",
    "#  格式：shutil.copy(来源路径，目标路径)\n",
    "#  返回值：返回目标路径\n",
    "# 拷贝的同时，可以给文件重命名\n",
    "\n",
    "rst = shutil.copy(\"/home/tlxy/dana.haha\", \"/home/tlxy/haha.haha\")\n",
    "print(rst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# copy2() 复制文件，保留元数据（文件信息）\n",
    "#  格式：shutil.copy2(来源路径，目标路径)\n",
    "#  返回值：返回目标路径\n",
    "#  注意：copy和copy2的唯一区别在于copy2复制文件时尽量保留元数据\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "haha.haha\n"
     ]
    }
   ],
   "source": [
    "# copyfile()将一个文件中的内容复制到另外一个文件当中\n",
    "#  格式：shutil.copyfile（'源路径','目标路径')\n",
    "#  返回值：无\n",
    "\n",
    "rst = shutil.copyfile(\"dana.haha\", \"haha.haha\")\n",
    "print(rst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/tlxy/dana/dana.haha\n"
     ]
    }
   ],
   "source": [
    "# move() 移动文件/文件夹\n",
    "#  格式：shutil.move(源路径，目标路径)\n",
    "#  返回值：目标路径！\n",
    "rst  = shutil.move(\"/home/tlxy/dana.haha\", \"/home/tlxy/dana\")\n",
    "print(rst)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 归档和压缩\n",
    "- 归档： 把多个文件或者文件夹合并到一个文件当中\n",
    "- 压缩： 用算法把多个文件或者文件夹无损或者有损合并到一个文件当中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/tlxy/tuling.zip\n"
     ]
    }
   ],
   "source": [
    "# make_archive() 归档操作\n",
    "#  格式:shutil.make_archive('归档之后的目录和文件名','后缀','需要归档的文件夹')\n",
    "#  返回值：归档之后的地址\n",
    "\n",
    "#help(shutil.make_archive)\n",
    "\n",
    "# 是想得到一个叫做tuling.zip的归档文件\n",
    "rst = shutil.make_archive(\"/home/tlxy/tuling\", \"zip\", \"/home/tlxy/dana\")\n",
    "print(rst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# unpack_archive() 解包操作\n",
    "# 格式：shutil.unpack_archive('归档文件地址','解包之后的地址')\n",
    "# 返回值：解包之后的地址\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# zip - 压缩包\n",
    "- 木块名称叫 zipfile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import zipfile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "#zipfile.ZipFile(file[, mode[, compression[, allowZip64]]])\n",
    "# 创建一个ZipFile对象，表示一个zip文件。参数file表示文件的路径或类文件对象(file-like object)；参数mode指示打开zip文件的模式，默认值为’r’，表示读已经存在的zip文件，也可以为’w’或’a’，’w’表示新建一个zip文档或覆盖一个已经存在的zip文档，’a’表示将数据附加到一个现存的zip文档中。参数compression表示在写zip文档时使用的压缩方法，它的值可以是zipfile. ZIP_STORED 或zipfile. ZIP_DEFLATED。如果要操作的zip文件大小超过2G，应该将allowZip64设置为True。\n",
    "\n",
    "zf = zipfile.ZipFile(\"/home/tlxy/tuling.zip\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<ZipInfo filename='dana.haha' compress_type=deflate filemode='-rw-rw-r--' file_size=41 compress_size=46>\n"
     ]
    }
   ],
   "source": [
    "# ZipFile.getinfo(name):\n",
    "#  获取zip文档内指定文件的信息。返回一个zipfile.ZipInfo对象，它包括文件的详细信息。将在下面 具体介绍该对象。\n",
    "\n",
    "rst = zf.getinfo(\"dana.haha\")\n",
    "print(rst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['haha.haha', '2-oop.ipynb', 'dana.haha']\n"
     ]
    }
   ],
   "source": [
    "# ZipFile.namelist()\n",
    "#  获取zip文档内所有文件的名称列表。\n",
    "\n",
    "nl = zf.namelist()\n",
    "print(nl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# ZipFile.extractall([path[, members[, pwd]]])\n",
    "#  解压zip文档中的所有文件到当前目录。参数members的默认值为zip文档内的所有文件名称列表，也可以自己设置，选择要解压的文件名称。\n",
    "\n",
    "rst = zf.extractall(\"/home/tlxy/dana\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(rst)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# random\n",
    "- 随机数\n",
    "- 所有的随机模块都是伪随机"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.4549137928915099\n"
     ]
    }
   ],
   "source": [
    "# random() 获取0-1之间的随机小数\n",
    "#  格式：random.random()\n",
    "#  返回值：随机0-1之间的小数\n",
    "\n",
    "print(random.random())\n",
    "\n",
    "# 作业： 利用random函数，注意是函数，生成0-100直接的整数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['0haha', '1haha', '2haha', '3haha', '4haha', '5haha', '6haha', '7haha', '8haha', '9haha']\n",
      "5haha\n"
     ]
    }
   ],
   "source": [
    "# choice() 随机返回序列中的某个值\n",
    "#  格式：random.choice(序列)\n",
    "#  返回值：序列中的某个值\n",
    "\n",
    "l = [str(i)+\"haha\" for i in range(10)]\n",
    "print(l)\n",
    "rst = random.choice(l)\n",
    "print(rst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "[4, 9, 0, 5, 3, 7, 8, 2, 1, 6]\n"
     ]
    }
   ],
   "source": [
    "# shuffle() 随机打乱列表\n",
    "#  格式：random.shuffle(列表)\n",
    "#  返回值：打乱顺序之后的列表\n",
    "\n",
    "l1 = [i for i in range(10)]\n",
    "print(l1)\n",
    "\n",
    "random.shuffle(l1)\n",
    "print(l1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "95\n",
      "Help on method randint in module random:\n",
      "\n",
      "randint(a, b) method of random.Random instance\n",
      "    Return random integer in range [a, b], including both end points.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# randint(a,b): 返回一个a到b之间的随机整数，包含a和b\n",
    "\n",
    "print(random.randint(0,100))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
